package com.duobaoyu.dby.supply.biz.client.resource.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.resource.admin.feign.RemoteResourceAddressAdminService;
import com.duobaoyu.dby.resource.common.admin.dto.area.*;
import com.duobaoyu.dby.resource.common.admin.to.area.AreaListTO;
import com.duobaoyu.dby.resource.common.admin.to.area.AreaTO;
import com.duobaoyu.dby.resource.common.admin.to.area.AreasBindDetailTO;
import com.duobaoyu.dby.resource.common.admin.to.area.ChannelAddressTO;
import com.duobaoyu.dby.resource.remote.common.dto.area.AddressDTO;
import com.duobaoyu.dby.resource.remote.common.dto.area.AddressInfoDTO;
import com.duobaoyu.dby.resource.remote.common.to.area.AddressInfoTO;
import com.duobaoyu.dby.resource.remote.common.to.area.AddressMappingTO;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceAddressAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AreaInfoBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.*;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.*;
import com.duobaoyu.dby.supply.biz.client.resource.converter.ResourceConverter;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import com.duobaoyu.dby.supply.core.task.bean.exporttask.ExportParam;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ResourceAddressAdminClientImpl
 * @createTime 11:20:00 2023/10/08
 */
@Component
@RequiredArgsConstructor
public class ResourceAddressAdminClientImpl implements ResourceAddressAdminClient {

    private final ResourceConverter resourceConverter;
    private final RemoteResourceAddressAdminService remoteResourceAddressAdminService;

    @Override
    public List<AddressMappingVO> selectAreaList(AddressQueryDTO addressQuery) {
        AddressDTO dto = resourceConverter.toAddress(addressQuery);
        R<List<AddressMappingTO>> result = remoteResourceAddressAdminService.getAreas(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, addressQuery);
        return resourceConverter.toAddressMappingList(result.getData());
    }

    @Override
    public Page<AreaListVO> areaPage(AreaQueryListDTO areaQueryList) {
        AreaListDTO dto = resourceConverter.toAreaList(areaQueryList);
        R<Page<AreaListTO>> result = remoteResourceAddressAdminService.getAreaList(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areaQueryList);
        return resourceConverter.toAreaListPage(result.getData());
    }

    @Override
    public List<AreaVO> getChannelAreas(ChannelAreasDTO channelAreas) {
        ChannelAreaDTO dto = resourceConverter.toChannelArea(channelAreas);
        R<List<AreaTO>> result = remoteResourceAddressAdminService.getChannelAreas(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelAreas);
        return resourceConverter.toAreaList(result.getData());
    }

    @Override
    public boolean bindAreas(AreaBindDTO areaBind) {
        AddressBindDTO dto = resourceConverter.toAddressBind(areaBind);
        R<Boolean> result = remoteResourceAddressAdminService.bindChannelAreas(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areaBind);
        return result.getData();
    }

    @Override
    public List<AreasBindDetailVO> areasBindDetail(AreasDetailDTO areasDetail) {
        AreasBindDetailDTO dto = resourceConverter.toAreasBindDetail(areasDetail);
        R<List<AreasBindDetailTO>> result = remoteResourceAddressAdminService.areasBindDetail(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areasDetail);
        return resourceConverter.toAreasBindDetailList(result.getData());
    }

    @Override
    public Map<String, String> getAreasMap(List<String> areaCodes) {
        R<Map<String, String>> result = remoteResourceAddressAdminService.getAreasMap(areaCodes, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areaCodes);
        return result.getData();
    }

    @Override
    public AreaInfoBO getChannelAddress(AreaInfoDTO areaInfo) {
        AddressInfoDTO dto = resourceConverter.toAddress(areaInfo);
        R<AddressInfoTO> result = remoteResourceAddressAdminService.getChannelAddress(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areaInfo);
        return resourceConverter.toAddress(result.getData());
    }

    @Override
    public AreaInfoBO getLocalAddress(AreaInfoDTO areaInfo) {
        areaInfo.setChannelCode(ChannelCodeEnum.LOCAL.getValue());
        AddressInfoDTO dto = resourceConverter.toAddress(areaInfo);
        R<AddressInfoTO> result = remoteResourceAddressAdminService.getLocalAddress(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, areaInfo);
        return resourceConverter.toAddress(result.getData());
    }

    @Override
    public Long getAddressMatchingCount() {
        R<Long> result = remoteResourceAddressAdminService.getAddressMatchingCount(SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, StringPool.EMPTY);
        return result.getData();
    }

    @Override
    public Boolean updateSupplyAddressById(AddressEditDTO addressEdit) {
        AreaEditDTO dto = AreaEditDTO.builder().supplyAddressId(addressEdit.getSupplyAddressId()).areaCode(addressEdit.getAreaCode()).build();
        R<Boolean> result = remoteResourceAddressAdminService.updateSupplyAddress(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, addressEdit);
        return result.getData();
    }

    @Override
    public Boolean updateMapAreaCodeById(AddressEditDTO addressEdit) {
        AreaEditDTO dto = AreaEditDTO.builder().supplyAddressId(addressEdit.getSupplyAddressId()).areaCode(addressEdit.getAreaCode()).build();
        R<Boolean> result = remoteResourceAddressAdminService.updateMapAreaCode(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, addressEdit);
        return result.getData();
    }

    @Override
    public Boolean addressLibraryExport(ExportParam<AreaListDTO> param) {
        R<Boolean> result = remoteResourceAddressAdminService.addressLibraryExport(param, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, param);
        return result.getData();
    }

    @Override
    public Page<ChannelAddressVO> channelAddressPage(ChannelAddressQueryDTO channelAddress) {
        ChannelAddressDTO dto = resourceConverter.toChannelAddress(channelAddress);
        R<Page<ChannelAddressTO>> result = remoteResourceAddressAdminService.getChannelAddress(dto, SecurityConstants.FROM_IN);
        RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.RESOURCE_SERVICE, channelAddress);
        return resourceConverter.toChannelAddressPage(result.getData());
    }
}
